package com.android.accessibility.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.android.accessibility.R;
import com.android.accessibility.ad.ForwardChecker;
import com.android.accessibility.base.BaseActivity;
import com.android.accessibility.base.BaseFragment;
import com.android.accessibility.fragment.DiscoverFragment;
import com.android.accessibility.fragment.OneKeyForwardFragment;
import com.android.accessibility.fragment.SettingsFragment;
import com.android.accessibility.fragment.VideoFragment;
import com.android.accessibility.stat.Stat;
import com.android.accessibility.upgrade.UpgradeManager;
import com.android.accessibility.utils.AppConfig;
import com.android.accessibility.utils.AppConfigRequest;
import com.android.accessibility.utils.HomePageSwitchHelper;

public class HomeActivity extends BaseActivity implements OnCheckedChangeListener {

    private HomePageSwitchHelper mHomePageSwitchHelper;
    private BaseFragment mOnekeyFragment;
    private BaseFragment mDiscoverFragment;
    private BaseFragment mVideoFragment;
    private BaseFragment mSettingsFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppConfigRequest request = new AppConfigRequest(this);
        request.check();
        ForwardChecker.get(this).init();
        Stat.get(this).init();
        setContentView(R.layout.home_layout);
        mHomePageSwitchHelper = new HomePageSwitchHelper(this);
        upgradeSelf();
        initView();
    }

    private void upgradeSelf() {
        UpgradeManager manager = new UpgradeManager(this);
        manager.checkUpgrade();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Stat.get(this).onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Stat.get(this).onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ForwardChecker.get(this).onDestroy();
    }

    private void initView() {
        AppConfig config = AppConfig.get(this);
        mOnekeyFragment = new OneKeyForwardFragment();
        mDiscoverFragment = new DiscoverFragment();
        mVideoFragment = new VideoFragment();
        mSettingsFragment = new SettingsFragment();
        RadioGroup rg = (RadioGroup) findViewById(R.id.bottom_bar);
        RadioButton rb = null;
        rb = (RadioButton) findViewById(R.id.check_forward);
        rb.setOnCheckedChangeListener(this);
        rb.setVisibility(config.isShowForward() ? View.VISIBLE : View.GONE);

        rb = (RadioButton) findViewById(R.id.check_discover);
        rb.setOnCheckedChangeListener(this);
        rb.setVisibility(config.isShowDiscover() ? View.VISIBLE : View.GONE);

        rb = (RadioButton) findViewById(R.id.check_video);
        rb.setOnCheckedChangeListener(this);
        rb.setVisibility(config.isShowWxVideo() ? View.VISIBLE : View.GONE);

        rb = (RadioButton) findViewById(R.id.check_settings);
        rb.setOnCheckedChangeListener(this);
        rb.setVisibility(config.isShowSettings() ? View.VISIBLE : View.GONE);

        // 设置第一个可见的页面为默认页面
        try {
            int childCount = rg.getChildCount();
            for (int index = 0; index < childCount; index++) {
                rb = (RadioButton) rg.getChildAt(index);
                if (rb.getVisibility() == View.VISIBLE) {
                    rb.setChecked(true);
                    break;
                }
            }
        } catch(Exception e) {
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            if (buttonView.getId() == R.id.check_forward) {
                mHomePageSwitchHelper.switchFragment(String.valueOf(buttonView.getId()), mOnekeyFragment);
            } else if (buttonView.getId() == R.id.check_discover) {
                mHomePageSwitchHelper.switchFragment(String.valueOf(buttonView.getId()), mDiscoverFragment);
            } else if (buttonView.getId() == R.id.check_video) {
                mHomePageSwitchHelper.switchFragment(String.valueOf(buttonView.getId()), mVideoFragment);
            }  else if (buttonView.getId() == R.id.check_settings) {
                mHomePageSwitchHelper.switchFragment(String.valueOf(buttonView.getId()), mSettingsFragment);
            }
        }
    }

    public void onBackPressed() {
        BaseFragment fragment = mHomePageSwitchHelper.getCurrentFragment();
        if (fragment.onBackPressed()) {
            return;
        }
        exitConfirm();
    }

    private void exitConfirm() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("退出");
        builder.setMessage("确认退出?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        builder.setNeutralButton("取消", null);
        if (ForwardChecker.get(this).hasAppWall()) {
            builder.setNegativeButton("应用推荐", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ForwardChecker.get(getBaseContext()).showAppWall();
                }
            });
        }
        builder.create().show();
    }
}
